<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>二进制安全比较字符串（从偏移位置比较指定长度）</title>
 </head>
 <body class="docs"><div id="layout">
  <div id="layout-content"><div id="function.substr-compare" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">substr_compare</h1>
  <p class="verinfo">(PHP 5, PHP 7)</p><p class="refpurpose"><span class="refname">substr_compare</span> &mdash; <span class="dc-title">二进制安全比较字符串（从偏移位置比较指定长度）</span></p>

 </div>
 
 <div class="refsect1 description" id="refsect1-function.substr-compare-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="methodname"><strong>substr_compare</strong></span>
    ( <span class="methodparam"><span class="type">string</span> <code class="parameter">$main_str</code></span>
   , <span class="methodparam"><span class="type">string</span> <code class="parameter">$str</code></span>
   , <span class="methodparam"><span class="type">int</span> <code class="parameter">$offset</code></span>
   [, <span class="methodparam"><span class="type">int</span> <code class="parameter">$length</code></span>
   [, <span class="methodparam"><span class="type">bool</span> <code class="parameter">$case_insensitivity</code><span class="initializer"> = <strong><code>FALSE</code></strong></span></span>
  ]] ) : <span class="type">int</span></div>

  <p class="para rdfs-comment">
   <span class="function"><strong>substr_compare()</strong></span> 从偏移位置 <code class="parameter">offset</code> 开始比较 <code class="parameter">main_str</code> 与 <code class="parameter">str</code>，比较长度为 <code class="parameter">length</code> 个字符。
  </p>
 </div>


 <div class="refsect1 parameters" id="refsect1-function.substr-compare-parameters">
  <h3 class="title">参数</h3>
  <p class="para">
   <dl>

    
     <dt>
<code class="parameter">main_str</code></dt>

     <dd>

      <p class="para">
       待比较的第一个字符串。
      </p>
     </dd>

    
    
     <dt>
<code class="parameter">str</code></dt>

     <dd>

      <p class="para">
       待比较的第二个字符串。
      </p>
     </dd>

    
    
     <dt>
<code class="parameter">offset</code></dt>

     <dd>

      <p class="para">
       比较开始的位置。如果为负数，则从字符串结尾处开始算起。
      </p>
     </dd>

    
    
     <dt>
<code class="parameter">length</code></dt>

     <dd>

      <p class="para">
       比较的长度。默认值为 <code class="parameter">str</code> 的长度与 <code class="parameter">main_str</code> 的长度减去位置偏移量 <code class="parameter">offset</code> 后二者中的较大者。
      </p>
     </dd>

    
    
     <dt>
<code class="parameter">case_insensitivity</code></dt>

     <dd>

      <p class="para">
       如果 <code class="parameter">case_insensitivity</code> 为 <strong><code>TRUE</code></strong>，比较将不区分大小写。
      </p>
     </dd>

    
   </dl>

  </p>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-function.substr-compare-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   如果 <code class="parameter">main_str</code> 从偏移位置 <code class="parameter">offset</code> 起的子字符串小于 <code class="parameter">str</code>，则返回小于 0 的数；如果大于 <code class="parameter">str</code>，则返回大于 0 的数；如果二者相等，则返回 0。如果 <code class="parameter">offset</code> 大于等于 <code class="parameter">main_str</code> 的长度或 <code class="parameter">length</code> 被设置为小于 1 的值（ PHP 5.5.11 之前的版本），<span class="function"><strong>substr_compare()</strong></span> 将打印出一条警告信息并且返回 <strong><code>FALSE</code></strong>。
  </p>
 </div>


 <div class="refsect1 changelog" id="refsect1-function.substr-compare-changelog">
  <h3 class="title">更新日志</h3>
  <p class="para">
   <table class="doctable informaltable">
    
     <thead>
      <tr>
       <th>版本</th>
       <th>说明</th>
      </tr>

     </thead>

     <tbody class="tbody">
      <tr>
       <td>5.5.11</td>
       <td>
        <code class="parameter">length</code> 可以是 <em>0</em>。
       </td>
      </tr>

      <tr>
       <td>5.1.0</td>
       <td>
        允许使用负数的 <code class="parameter">offset</code> 参数。
       </td>
      </tr>

     </tbody>
    
   </table>

  </p>
 </div>


 <div class="refsect1 examples" id="refsect1-function.substr-compare-examples">
  <h3 class="title">范例</h3>
  <p class="para">
   <div class="example" id="example-5991">
    <p><strong>Example #1 <span class="function"><strong>substr_compare()</strong></span> 范例</strong></p>
    <div class="example-contents">
<div class="phpcode"><pre><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">echo&nbsp;</span><span style="color: #0000BB">substr_compare</span><span style="color: #007700">(</span><span style="color: #DD0000">"abcde"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"bc"</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">1</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">2</span><span style="color: #007700">);&nbsp;</span><span style="color: #FF8000">//&nbsp;0<br /></span><span style="color: #007700">echo&nbsp;</span><span style="color: #0000BB">substr_compare</span><span style="color: #007700">(</span><span style="color: #DD0000">"abcde"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"de"</span><span style="color: #007700">,&nbsp;-</span><span style="color: #0000BB">2</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">2</span><span style="color: #007700">);&nbsp;</span><span style="color: #FF8000">//&nbsp;0<br /></span><span style="color: #007700">echo&nbsp;</span><span style="color: #0000BB">substr_compare</span><span style="color: #007700">(</span><span style="color: #DD0000">"abcde"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"bcg"</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">1</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">2</span><span style="color: #007700">);&nbsp;</span><span style="color: #FF8000">//&nbsp;0<br /></span><span style="color: #007700">echo&nbsp;</span><span style="color: #0000BB">substr_compare</span><span style="color: #007700">(</span><span style="color: #DD0000">"abcde"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"BC"</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">1</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">2</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">true</span><span style="color: #007700">);&nbsp;</span><span style="color: #FF8000">//&nbsp;0<br /></span><span style="color: #007700">echo&nbsp;</span><span style="color: #0000BB">substr_compare</span><span style="color: #007700">(</span><span style="color: #DD0000">"abcde"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"bc"</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">1</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">3</span><span style="color: #007700">);&nbsp;</span><span style="color: #FF8000">//&nbsp;1<br /></span><span style="color: #007700">echo&nbsp;</span><span style="color: #0000BB">substr_compare</span><span style="color: #007700">(</span><span style="color: #DD0000">"abcde"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"cd"</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">1</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">2</span><span style="color: #007700">);&nbsp;</span><span style="color: #FF8000">//&nbsp;-1<br /></span><span style="color: #007700">echo&nbsp;</span><span style="color: #0000BB">substr_compare</span><span style="color: #007700">(</span><span style="color: #DD0000">"abcde"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"abc"</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">5</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">1</span><span style="color: #007700">);&nbsp;</span><span style="color: #FF8000">//&nbsp;warning<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</pre></div>
    </div>

   </div>
  </p>
 </div>


 <div class="refsect1 seealso" id="refsect1-function.substr-compare-seealso">
  <h3 class="title">参见</h3>
  <p class="para">
   <ul class="simplelist">
    <li class="member"><span class="function"><a href="strncmp.html" class="function" rel="rdfs-seeAlso">strncmp()</a> - 二进制安全比较字符串开头的若干个字符</span></li>
   </ul>
  </p>
 </div>

 
</div></div></div></body></html>